package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.PhotoAlbum;
import com.miku.blog.domain.dto.PhotoAlbumDTO;
import com.miku.blog.domain.pojo.LoginUser;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.domain.vo.PageVo;
import com.miku.blog.domain.vo.PhotoAlbumsVo;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.exception.SystemException;
import com.miku.blog.service.PhotoAlbumService;
import com.miku.blog.mapper.PhotoAlbumMapper;
import com.miku.blog.service.PhotoService;
import com.miku.blog.utils.BeanCopyUtils;
import com.miku.blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Miku
 * @description 针对表【tb_photo_album(相册)】的数据库操作Service实现
 * @createDate 2022-06-09 01:30:28
 */
@Service
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumMapper, PhotoAlbum>
        implements PhotoAlbumService {

    @Autowired
    private PhotoService photoService;


    @Override
    public ResponseResult getAlbums() {
        LambdaQueryWrapper<PhotoAlbum> queryWrapper = new LambdaQueryWrapper<>();

        checkAdmin(queryWrapper);

        queryWrapper.eq(PhotoAlbum::getIsDelete, SystemConstance.NOT_DELETE);
//                .eq(PhotoAlbum::getStatus,SystemConstance.STATUS_PUBLIC);
        List<PhotoAlbum> photoAlbumList = list(queryWrapper);
        List<PhotoAlbumsVo> photoAlbumsVos = BeanCopyUtils.copyListByType(photoAlbumList, PhotoAlbumsVo.class);
        photoAlbumsVos.stream()
                .peek(photoAlbumsVo -> {
                    long count = photoService.getPhotoCountByAlbumId(photoAlbumsVo.getId());
                    photoAlbumsVo.setPhotoCount((int) count);
                }).collect(Collectors.toList());
        return ResponseResult.ok().setData(photoAlbumsVos);
    }

    @Override
    public ResponseResult getPhotoAlbums(Integer current, Integer size, String keywords,Integer status) {
        Page<PhotoAlbum> page = new Page<>(current, size);
        LambdaQueryWrapper<PhotoAlbum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PhotoAlbum::getIsDelete, SystemConstance.NOT_DELETE)
                .eq(Objects.nonNull(status),PhotoAlbum::getStatus,status)
                .like(StringUtils.hasText(keywords),PhotoAlbum::getAlbumName,keywords);

        page(page, queryWrapper);
        List<PhotoAlbumsVo> photoAlbumsDTOS = BeanCopyUtils.copyListByType(page.getRecords(), PhotoAlbumsVo.class);
        photoAlbumsDTOS.stream()
                .peek(dto -> {
                    long count = photoService.getPhotoCountByAlbumId(dto.getId());
                    dto.setPhotoCount((int) count);
                }).collect(Collectors.toList());
        PageVo<PhotoAlbumsVo> pageVo = new PageVo<>(page.getTotal(), photoAlbumsDTOS);
        return ResponseResult.ok().setData(pageVo);
    }


    @Override
    public ResponseResult getPhotoAlbumIdInfo(Integer albumId) {
        LambdaQueryWrapper<PhotoAlbum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PhotoAlbum::getId, albumId)
                .eq(PhotoAlbum::getIsDelete, SystemConstance.NOT_DELETE);
        PhotoAlbum photoAlbum = getOne(queryWrapper);
        PhotoAlbumsVo photoAlbumsVo = BeanCopyUtils.copyObjectByType(photoAlbum, PhotoAlbumsVo.class);
        long count = photoService.getPhotoCountByAlbumId(photoAlbumsVo.getId());
        photoAlbumsVo.setPhotoCount((int) count);
        return ResponseResult.ok().setData(photoAlbumsVo);
    }

    @Override
    public ResponseResult getPhotoAlbumsInfo() {
        LambdaQueryWrapper<PhotoAlbum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PhotoAlbum::getIsDelete, SystemConstance.NOT_DELETE);
        List<PhotoAlbum> photoAlbumList = list(queryWrapper);
        List<PhotoAlbumsVo> photoAlbumsVos = BeanCopyUtils.copyListByType(photoAlbumList, PhotoAlbumsVo.class);
        return ResponseResult.ok().setData(photoAlbumsVos);
    }


    @Override
    public ResponseResult addPhotoAlbum(PhotoAlbum photoAlbum) {
        String albumCover = photoAlbum.getAlbumCover();
        String albumDesc = photoAlbum.getAlbumDesc();
        String albumName = photoAlbum.getAlbumName();
        Integer status = photoAlbum.getStatus();
        if (!StringUtils.hasText(albumCover) || !StringUtils.hasText(albumDesc) || !StringUtils.hasText(albumName)
                || Objects.isNull(status)
                || (!SystemConstance.STATUS_PUBLIC.equals(status) && !SystemConstance.STATUS_PRIVATE.equals(status))
        ) {
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }

        Integer id = photoAlbum.getId();
        if (Objects.isNull(id)) {
            if (!save(photoAlbum))
                return new ResponseResult(HttpStatusEnum.INSERT_DATA_ERROR);
        } else {
            if (!updateById(photoAlbum))
                return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    @Transactional
    @Override
    public ResponseResult movePhoto(PhotoAlbumDTO photoAlbumDTO) {
        List<Integer> photoIdList = photoAlbumDTO.getPhotoIdList();
        Integer albumId = photoAlbumDTO.getAlbumId();
        if (Objects.isNull(albumId) || Objects.isNull(photoIdList) || photoIdList.size() <= 0){
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }
        photoIdList.stream()
                .peek(photoId -> {
                    if (!photoService.updatePhotoById(albumId,photoId)) {
                        throw new SystemException(HttpStatusEnum.UPDATE_DATA_ERROR);
                    }
                }).collect(Collectors.toList());
        return ResponseResult.ok();
    }


    @Override
    public ResponseResult delAlbums(Integer albumId) {

        photoService.delPhotoByAlbumId(albumId);
        if (!removeById(albumId)){
            return new ResponseResult(HttpStatusEnum.REMOVE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    private void checkAdmin(LambdaQueryWrapper<PhotoAlbum> queryWrapper){
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (!SystemConstance.ROLE_ADMIN_ID.equals(loginUser.getRole().getId())) {
                queryWrapper.eq(PhotoAlbum::getStatus, SystemConstance.STATUS_PUBLIC);
            }
        } catch (Exception exception) {
            queryWrapper.eq(PhotoAlbum::getStatus, SystemConstance.STATUS_PUBLIC);
        }
    }
}




